<?php 
namespace app\api\logic;
require_once env('root_path')."vendor/tp_wechat/Wechat.php";
require_once env('root_path')."vendor/tp_wechat/base/WechatBase.php";
use vendor\tp_wechat\Wechat as WechatObj;
use vendor\tp_wechat\base\WechatBase;
use think\Db;
class Publics extends Base
{

	function __construct()
	{
	}

  //分类信息
	public function category()
	{
        $category = Db::name('industry')->select();
         $this->res_data['Code'] = 0;
         $this->res_data['Data'] =$category;
         return $this->res_data;
	}
    public function userInfo($input){
        $uid = isset($input['user_id']) ? $input['user_id']:0;
        if (!$uid){
            $this->return_data['Msg'] = " 参数错误";
            return $this->return_data;
        }
        $user = Db::name('user')->where([['id','=',$uid]])->find();
        if (!$user){
            $this->return_data['Msg'] = " 无该用户";
            return $this->return_data;
        }
        $user['createdate'] = date('Y-m-d',$user['create_time']);
        if ($user['is_teacher']==1) {
            $teacher = Db::name('teacher')->where(['user_id'=>$user['id']])->field('id,name,avatar')->find();
            if (!$teacher) {
                $this->return_data['Msg'] = " 尚未添加老师信息，请联系管理员";
                return $this->return_data;
            }
            $user['teacher_avatar'] = config('upload_domain').$teacher['avatar'];
            $user['teacher_name'] = $teacher['name'];
            $user['teacher_classes'] = Db::name('classes')->where(['master_teacher'=>$teacher['id']])->value('group_concat(name) as classes');
        } else {
            $user['teacher_name'] = '';
            $user['teacher_avatar'] = '';
            $user['teacher_classes'] = '';
        }
        $this->return_data['Code'] = 0;
        $this->return_data['Msg'] = "成功";
        $this->return_data['Data'] = $user;
        return $this->return_data;
    }


    /**
     * 发送微信模版通知
     * @DateTime 2020-09-14T19:46:14+0800
     * @param    [type]                   $openid      [description]
     * @param    [type]                   $template_id [description]
     * @param    [type]                   $url         [description]
     * @param    [type]                   $data        [description]
     * data数据：
     * {
           "touser":"OPENID",
           "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
           "url":"http://weixin.qq.com/download",  
           "miniprogram":{
             "appid":"xiaochengxuappid12345",
             "pagepath":"index?foo=bar"
           },          
           "data":{
                   "first": {
                       "value":"恭喜你购买成功！",
                       "color":"#173177"
                   },
                   "keyword1":{
                       "value":"巧克力",
                       "color":"#173177"
                   },
                   "keyword2": {
                       "value":"39.8元",
                       "color":"#173177"
                   },
                   "keyword3": {
                       "value":"2014年9月22日",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"欢迎再次购买！",
                       "color":"#173177"
                   }
           }
       }
     * @param    string                   $topcolor    [description]
     * @return   [type]                                [description]
     */
    public function send_template_message($template_id = '', $page = '', $data = [], $openid = '')
    {
            write_log($page);
      if (!$template_id || !$page || !$data || !$openid) {
        return false;
      }
      $arr = [
          'url' => '',
          'touser' => $openid,
          'template_id' => $template_id,
          'miniprogram' => ['appid' => config('appid'),'pagepath'=>$page],
          'data' => $data,
          'pagepath' => $page,
          // 'appid' => config('appid'),
      ];
      $option = ['appid'=>config('fwhappid'),'appsecret'=>config('fwhappsecret')];
      $messageObj = (new WechatObj($option))->message($option);
      $WechatBase = new WechatBase($option);
      $access_token = '';
      $access_token = $WechatBase->getToken(1);
      $res = $messageObj->sendTemplateMessage(json_encode($arr),$access_token);
      return $res;
    }
}

 ?>
